<?php

use Doraemon\tools\ActionLog;


/**
 * 门店提问
 * Class ListAction
 */
class AskAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REALNAME;

    /**
     * @throws Exception
     */
    public function main()
    {
        $storeId = $this->getPost('store_id', 0);
        $askId = $this->getPost('question_id', 0);
        $msg = $this->getPost('msg', '');
        $uid = $this->uid;

        $storeUid = \Biz\service\Store\StoreBasicService::getStoreUidById($storeId);

        if (!strlen($msg)) throw new \Exception('内容不能为空');

        if ($askId) {//回答
            if (mb_strlen($msg) > 500) throw new \Exception('最多支持输入 500 字，请重新编辑');
            \Biz\service\Store\StoreAskService::addAnswer($storeId, $askId, $uid, $storeUid, $msg);
        } else {//提问
            if (mb_strlen($msg) > 50) throw new \Exception('最多支持输入 50 字，请重新编辑');
            \Biz\service\Store\StoreAskService::addAsk($storeId, $storeUid, $uid, $msg);
        }

        ActionLog::log($this->uid, ['store_id' => $storeId, 'question_id' => $askId], ActionLog::STORE_ASK);

        $this->setSuccess('success');
    }

}